package com.jbm.jbmsupplier.zp.interfaces;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.jbm.jbmsupplier.zp.entity.Buddy_ListData;
import com.jbm.jbmsupplier.zp.entity.ChatMsgEntity;
import com.jbm.jbmsupplier.zp.entity.Recent_ContactsData;

import java.util.ArrayList;

/**
 * Created by 家帮忙 on 2017/7/19.
 */

public class SolistGroads implements SqlistInterface {


    @Override
    public void createGroadTable(SQLiteDatabase db, String tableName) {//聊天表
        db.execSQL("create table " + tableName + "(id int identity(1,1) primary key,name text,head_portrait text,date text,texts text,time text,to_id text,local_id text,sedname text,Route text,MediaName text,AudioTime text,UnreadMsg text,address text,photo text,flag text,cgid text,tags text)");

    }

    @Override
    public void crateGroad(SQLiteDatabase db, String tableName) {//联系人表

        db.execSQL("create table " + tableName + "(id int identity(1,1) primary key,name text,head_portrait text,time text,msg text,unreadmsg text,cgid text,tag text,toid text)");    }

    @Override
    public void insertGroadtable(SQLiteDatabase db, String tableName, String name, String head_portrait, String time, String msg, int unreadmsg, int cgid, int tag,int toid) {
        db.execSQL("insert into " + tableName + " (name ,head_portrait ,time,msg,unreadmsg,cgid ,tag,toid) values (?,?,?,?,?,?,?,?)",new String[]{name ,head_portrait ,time,msg,unreadmsg+"",cgid+"" ,tag+"",toid+""});

    }

    @Override
    public void insertGroadTable(SQLiteDatabase db, String tableName, String name , String head_portrait , String  date , String  texts, String  time , int to_id , int local_id,int orderid,double amount ,String sedname ,String Route,String MediaName ,String AudioTime ,int UnreadMsg ,String address ,String photo , int flag , int cgid, int tags,String isboo,int login) {
        //存储群组聊天数据
            db.execSQL("insert into " + tableName + " (name ,head_portrait ,date,texts,time,to_id ,local_id,orderid,amount,sedname,Route,MediaName,AudioTime,UnreadMsg,address, photo,flag,cgid ,tags,isboo,login) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",new String[]{name,head_portrait,date,texts,time,to_id+"",local_id+"",orderid+"",amount+"",sedname , Route, MediaName , AudioTime , UnreadMsg +"", address , photo ,flag+"",cgid+"",tags+"",isboo,login+""});
    }

    @Override
    public ArrayList<ChatMsgEntity> showGroadTable(SQLiteDatabase db, String tableName, int cgid,int login) {
        ArrayList<ChatMsgEntity> list = new ArrayList<>();
        ChatMsgEntity chatMsgEntity = null;
//        Cursor cursor = db.rawQuery("select * from "+tableName+" where to_id in(?,?) and local_id in(?,?)", new String[]{String.valueOf(to_id), String.valueOf(local_id), String.valuOf(to_id), String.valueOf(local_id)});
        Cursor cursor = db.rawQuery("select * from " + tableName + " where cgid = ? and login = ? order by date asc", new String[]{cgid+"",login+""});
        cursor.moveToPrevious();
        while (cursor.moveToNext()) {
            chatMsgEntity = new ChatMsgEntity();
            chatMsgEntity.setName(cursor.getString(cursor.getColumnIndex("name")));
            chatMsgEntity.setHead_portrait(cursor.getString(cursor.getColumnIndex("head_portrait")));
            chatMsgEntity.setDate(cursor.getString(cursor.getColumnIndex("date")));
            chatMsgEntity.setText(cursor.getString(cursor.getColumnIndex("texts")));
           chatMsgEntity.setTime(cursor.getString(cursor.getColumnIndex("time")));
           chatMsgEntity.setTo_id(cursor.getInt(cursor.getColumnIndex("to_id")));
            chatMsgEntity.setLocal_id(cursor.getInt(cursor.getColumnIndex("local_id")));
            chatMsgEntity.setSedtext(cursor.getString(cursor.getColumnIndex("sedname")));
            chatMsgEntity.setRoute(cursor.getString(cursor.getColumnIndex("Route")));
            chatMsgEntity.setMediaName(cursor.getString(cursor.getColumnIndex("MediaName")));
            chatMsgEntity.setAudioTime(cursor.getString(cursor.getColumnIndex("AudioTime")));
            chatMsgEntity.setUnreadMsg(cursor.getInt(cursor.getColumnIndex("UnreadMsg")));
            chatMsgEntity.setAddress(cursor.getString(cursor.getColumnIndex("address")));
            chatMsgEntity.setPhoto(cursor.getString(cursor.getColumnIndex("photo")));
            chatMsgEntity.setFlag(cursor.getInt(cursor.getColumnIndex("flag")));
            chatMsgEntity.setCgid(cursor.getInt(cursor.getColumnIndex("cgid")));
            chatMsgEntity.setTags(cursor.getInt(cursor.getColumnIndex("tags")));
            chatMsgEntity.setOrderid(cursor.getInt(cursor.getColumnIndex("orderid")));
            chatMsgEntity.setAmount(cursor.getDouble(cursor.getColumnIndex("amount")));
            chatMsgEntity.setIsboolean(cursor.getString(cursor.getColumnIndex("isboo")));
            list.add(chatMsgEntity);
        }
        return list;
    }

    @Override
    public Recent_ContactsData showGroadTableid(SQLiteDatabase db, String tableName, int cgid) {
        Recent_ContactsData recent_contactsData = null;
        Cursor cursor = db.rawQuery("select * from " + tableName + " where cgid = ?", new String[]{cgid+""});
        cursor.moveToPrevious();
        while (cursor.moveToNext()) {
            recent_contactsData = new Recent_ContactsData();
            recent_contactsData.setName(cursor.getString(cursor.getColumnIndex("name")));
        }
        return recent_contactsData;

    }

    @Override
    public ArrayList<Recent_ContactsData> showGroad(SQLiteDatabase db, String tableName, int cgid) {
        ArrayList<Recent_ContactsData> list = new ArrayList<>();
        Recent_ContactsData recent_contactsData = null;
//        Cursor cursor = db.rawQuery("select * from "+tableName+" where to_id in(?,?) and local_id in(?,?)", new String[]{String.valueOf(to_id), String.valueOf(local_id), String.valuOf(to_id), String.valueOf(local_id)});
        Cursor cursor = db.rawQuery("select * from " + tableName + " where  toid = ?  order by time desc ", new String[]{cgid+""});/*order by toid asc*/
        cursor.moveToPrevious();
        while (cursor.moveToNext()) {
            recent_contactsData = new Recent_ContactsData();
            recent_contactsData.setName(cursor.getString(cursor.getColumnIndex("name")));
            recent_contactsData.setHead_portrait(cursor.getString(cursor.getColumnIndex("head_portrait")));
            recent_contactsData.setTime(cursor.getString(cursor.getColumnIndex("time")));
            recent_contactsData.setMsg(cursor.getString(cursor.getColumnIndex("msg")));
            recent_contactsData.setUnreadMsg(cursor.getInt(cursor.getColumnIndex("unreadmsg")));
            recent_contactsData.setCgid(cursor.getInt(cursor.getColumnIndex("cgid")));
            recent_contactsData.setTag(cursor.getInt(cursor.getColumnIndex("tag")));
            recent_contactsData.setTo_id(cursor.getInt(cursor.getColumnIndex("toid")));
            list.add(recent_contactsData);
        }

        return list;
    }

    @Override
    public void setdelete(SQLiteDatabase db, String tableName, int cgid,int loginid) {
        db.execSQL("delete from " + tableName + " where cgid = ? and toid = ?", new String[]{cgid+"",loginid+""});

    }

    @Override
    public int SelectUnreadmsg(SQLiteDatabase db, String tableName, int cgid) {
        Cursor cursor = db.rawQuery("select count(UnreadMsg) from "+tableName+" where UnreadMsg = '1'and cgid = ?",new String[]{cgid+""});
        cursor.moveToFirst();
        return  cursor.getInt(0);
    }

    @Override
    public void UpdateReadmsg(SQLiteDatabase db, String tableName, int cgid) {
        db.execSQL("update " + tableName + " set UnreadMsg = '0' where UnreadMsg = '1' and cgid =  "+cgid);
    }

    @Override
    public void UpdateErrorMsg(SQLiteDatabase db, String tableName,String isboolean, int cgid, String data,int login) {
        db.execSQL("update " + tableName + " set isboo = '"+isboolean+"'"+" where date='"+data+"' and login ='"+login+"'");    }

    @Override
    public void StoringBuddydata(SQLiteDatabase db,String tableName, int m_account_id, String nickname, String account, String mobile, String remark, String head_portrait, String autograph, String region, String Circle_friends, int isfriends, int loginid) {
        db.execSQL("insert into " + tableName + " (m_account_id,nickname,account,mobile,remark,head_portrait,autograph,region,Circle_friends,isfriends,loginid) values (?,?,?,?,?,?,?,?,?,?,?)", new Object[]{m_account_id,nickname,account,mobile,remark,head_portrait,autograph,region,Circle_friends,isfriends,loginid+""});

    }

    @Override
    public void StoringChatData(SQLiteDatabase db,String tableName, int to_id, String myhead_portrait, String text, String Date, String Name, String Route, String MediaName, String AudioTime, String payDate, String payMoney, String payMode, String payState, String code, String memo, int flag, int UnreadMsg, String isboolean, int loginid) {
        db.execSQL("insert into " + tableName + " (to_id,myhead_portrait,Msg,Date,Name,Route,MediaName,AudioTime,payDate,payMoney,payMode,payState,code,memo,flag,UnreadMsg,isboolean,loginid) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", new Object[]{to_id,myhead_portrait, text, Date,Name,Route,MediaName,AudioTime,payDate,payMoney,payMode,payState,code,memo,flag, UnreadMsg,isboolean,loginid+""});

    }

    @Override
    public void StoringRecentID(SQLiteDatabase db, String tableName, int contactsID, int login, String date) {
        db.execSQL("insert into " + tableName + " (contactsID,login,date) values (?,?,?)", new Object[]{contactsID,login+"",date});

    }

    @Override
    public ArrayList<String> showaRecentContactsid(SQLiteDatabase db, String tableName, int toid, int loginin) {
        ArrayList<String> list = new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from " + tableName + " where contactsID = ? and login = ? order by date desc", new String[]{toid+"",loginin+""});//order by id desc
        cursor.moveToPrevious();
        //获取name列的索引
        while (cursor.moveToNext()) {
            String st = cursor.getString(cursor.getColumnIndex("contactsID"));
            list.add(st);
        }
        return list;
    }

    @Override
    public ArrayList<String> showaRecentContactsids(SQLiteDatabase db, String tableName, int login) {
        ArrayList<String> list = new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from " + tableName + " where login="+login, null);//order by id desc
        cursor.moveToPrevious();
        //获取name列的索引
        while (cursor.moveToNext()) {
            String st = cursor.getString(cursor.getColumnIndex("contactsID"));
            list.add(st);
        }
        return list;
    }

    @Override
    public ArrayList<Recent_ContactsData> showRecentContactslast(SQLiteDatabase db, String tableName, int loginid, int toid) {
        ArrayList<Recent_ContactsData> list = new ArrayList<>();
        Recent_ContactsData recentContactsData = null;
        Cursor cursor = db.rawQuery("select * from " + tableName + " where loginid= ? and to_id = ? order by Date desc ", new String[]{loginid+"",toid+""});//limit 0,1
        cursor.moveToPrevious();
        while (cursor.moveToNext()) {
            recentContactsData = new Recent_ContactsData();
            recentContactsData.setName(cursor.getString(cursor.getColumnIndex("Name")));
            recentContactsData.setMsg(cursor.getString(cursor.getColumnIndex("Msg")));
            recentContactsData.setTime(cursor.getString(cursor.getColumnIndex("Date")));
            recentContactsData.setUnreadMsg(cursor.getInt(cursor.getColumnIndex("UnreadMsg")));
            recentContactsData.setHead_portrait(cursor.getString(cursor.getColumnIndex("myhead_portrait")));
            list.add(recentContactsData);
        }
        return list;
    }

    @Override
    public ArrayList<ChatMsgEntity> showChatData(SQLiteDatabase db, String tableName, int loginid, int toid) {
        ArrayList<ChatMsgEntity> list = new ArrayList<>();
        ChatMsgEntity chatMsgEntity = null;
//        Cursor cursor = db.rawQuery("select * from "+tableName+" where to_id in(?,?) and local_id in(?,?)", new String[]{String.valueOf(to_id), String.valueOf(local_id), String.valuOf(to_id), String.valueOf(local_id)});
        Cursor cursor = db.rawQuery("select * from " + tableName + " where loginid = ? and to_id = ? order by Date asc", new String[]{loginid+"",""+toid});
        cursor.moveToPrevious();
        while (cursor.moveToNext()) {
            chatMsgEntity = new ChatMsgEntity();
            chatMsgEntity.setTo_id(cursor.getInt(cursor.getColumnIndex("to_id")));
            chatMsgEntity.setHead_portrait(cursor.getString(cursor.getColumnIndex("myhead_portrait")));
            chatMsgEntity.setText(cursor.getString(cursor.getColumnIndex("Msg")));
            chatMsgEntity.setDate(cursor.getString(cursor.getColumnIndex("Date")));
            chatMsgEntity.setName(cursor.getString(cursor.getColumnIndex("Name")));
            chatMsgEntity.setRoute(cursor.getString(cursor.getColumnIndex("Route")));
            chatMsgEntity.setMediaName(cursor.getString(cursor.getColumnIndex("MediaName")));
            chatMsgEntity.setAudioTime(cursor.getString(cursor.getColumnIndex("AudioTime")));
            chatMsgEntity.setPayDate(cursor.getString(cursor.getColumnIndex("payDate")));
            chatMsgEntity.setPayMoney(cursor.getString(cursor.getColumnIndex("payMoney")));
            chatMsgEntity.setPayMode(cursor.getString(cursor.getColumnIndex("payMode")));
            chatMsgEntity.setPayState(cursor.getString(cursor.getColumnIndex("payState")));
            chatMsgEntity.setCode(cursor.getString(cursor.getColumnIndex("code")));
            chatMsgEntity.setMemo(cursor.getString(cursor.getColumnIndex("memo")));
            chatMsgEntity.setFlag(cursor.getInt(cursor.getColumnIndex("flag")));
            chatMsgEntity.setIsboolean(cursor.getString(cursor.getColumnIndex("isboolean")));
            list.add(chatMsgEntity);
        }
        return list;
    }

    @Override
    public ArrayList<Buddy_ListData> showdata(SQLiteDatabase db, String tableName, int loginid) {
        ArrayList<Buddy_ListData> list = new ArrayList<>();
        Buddy_ListData buddy_listData = null;
        Cursor cursor = db.rawQuery("select * from " + tableName + " where loginid = "+ loginid, null);//desc:降序，asc：升序" where isfriends = 1
        cursor.moveToPrevious();
        //获取name列的索引
        while (cursor.moveToNext()) {
            buddy_listData = new Buddy_ListData();
            buddy_listData.setM_account_id(cursor.getInt(cursor.getColumnIndex("m_account_id")));
            buddy_listData.setNickname(cursor.getString(cursor.getColumnIndex("nickname")));
            buddy_listData.setAccount(cursor.getString(cursor.getColumnIndex("account")));
            buddy_listData.setMobile(cursor.getString(cursor.getColumnIndex("mobile")));
            buddy_listData.setRemark(cursor.getString(cursor.getColumnIndex("remark")));
            buddy_listData.setHead_portrait(cursor.getString(cursor.getColumnIndex("head_portrait")));
            buddy_listData.setAutograph(cursor.getString(cursor.getColumnIndex("autograph")));
            buddy_listData.setRegion(cursor.getString(cursor.getColumnIndex("region")));
            buddy_listData.setCircle_friends(cursor.getString(cursor.getColumnIndex("Circle_friends")));
            buddy_listData.setAge(cursor.getString(cursor.getColumnIndex("age")));
            buddy_listData.setSex(cursor.getString(cursor.getColumnIndex("sex")));
            buddy_listData.setIsfriends(cursor.getInt(cursor.getColumnIndex("isfriends")));
            buddy_listData.setLoginid(cursor.getInt(cursor.getColumnIndex("loginid")));
            list.add(buddy_listData);
        }
        return list;
    }

    @Override
    public Buddy_ListData showFriendsToID(SQLiteDatabase db, String tableName, int m_account_id, int loginid) {
        Buddy_ListData buddy_listData = null;
        Cursor cursor = db.rawQuery("select * from " + tableName + " where m_account_id = "+m_account_id+" and loginid ="+loginid, null);
        cursor.moveToPrevious();
        //获取name列的索引
        while (cursor.moveToNext()) {
            buddy_listData = new Buddy_ListData();
            buddy_listData.setM_account_id(cursor.getInt(cursor.getColumnIndex("m_account_id")));
            buddy_listData.setNickname(cursor.getString(cursor.getColumnIndex("nickname")));
            buddy_listData.setAccount(cursor.getString(cursor.getColumnIndex("account")));
            buddy_listData.setMobile(cursor.getString(cursor.getColumnIndex("mobile")));
            buddy_listData.setRemark(cursor.getString(cursor.getColumnIndex("remark")));
            buddy_listData.setHead_portrait(cursor.getString(cursor.getColumnIndex("head_portrait")));
            buddy_listData.setAutograph(cursor.getString(cursor.getColumnIndex("autograph")));
            buddy_listData.setRegion(cursor.getString(cursor.getColumnIndex("region")));
            buddy_listData.setCircle_friends(cursor.getString(cursor.getColumnIndex("Circle_friends")));
            buddy_listData.setAge(cursor.getString(cursor.getColumnIndex("age")));
            buddy_listData.setSex(cursor.getString(cursor.getColumnIndex("sex")));
            buddy_listData.setIsfriends(cursor.getInt(cursor.getColumnIndex("isfriends")));

        }
        return buddy_listData;
    }


}
